package com.zx.wangyixinwen.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.bean.Found;
import com.zx.wangyixinwen.imageCache.My;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/15.
 */
public class FoundAdapter extends BaseAdapter {

    private List<Found.DataEntity.ChannelGroupsEntity> datas;
    private Context mContext;
    private RequestQueue queue;
    private ImageLoader imageLoader;

    public FoundAdapter(Context c) {
        mContext = c;
        queue = Volley.newRequestQueue(mContext);
        imageLoader = new ImageLoader(queue, new My());


    }

    public void initData(List<Found.DataEntity.ChannelGroupsEntity> datas) {
        if (this.datas == null) {
            this.datas = new ArrayList<Found.DataEntity.ChannelGroupsEntity>();

        } else {
            this.datas.clear();
        }
        this.datas.addAll(datas);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Found.DataEntity.ChannelGroupsEntity getItem(int position) {
        return datas != null && datas.size() > 0 ? datas.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.found_adapter, null);
            holder = new ViewHolder();
            holder.imageViewIC = (ImageView) convertView.findViewById(R.id.found_imageView);
            holder.textViewTT = (TextView) convertView.findViewById(R.id.found_textView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Found.DataEntity.ChannelGroupsEntity hot = getItem(position);
        String s = hot.getData().getCover_image_url();

        if (hot != null) {
            ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(
                    holder.imageViewIC, R.mipmap.refresh, R.mipmap.refresh);

            imageLoader.get(s, imageListener);

            holder.textViewTT.setText(hot.getData().getName());

        }


        return convertView;
    }

    private class ViewHolder {
        private ImageView imageViewIC;
        private TextView textViewTT;

    }
}